package com.qiaoyanqing.ximalaya.discovermodule.discover_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRankSecondAnchorAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRankSecondAnchorService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondAnchorEntity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondDataAnchorEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.qiaoyanqing.ximalaya.util.MyLog;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DiscoverRankAnchorActivity extends AppCompatActivity implements Callback<DiscoverRankSecondAnchorEntity>, AbsListView.OnScrollListener {
    private ListView listView;
    private DiscoverRankSecondAnchorAdapter adapter;
    private List<DiscoverRankSecondDataAnchorEntity>list;
    private String contentType;
    private String key;
    private int pageSize=20;
    private String title;
    private TextView textView;
    private boolean isBottom;
    private int totalCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank_anchor);
        initView();
    }

    private void initView() {

        listView=(ListView)findViewById(R.id.rank_anchor_list_view);
        textView=(TextView)findViewById(R.id.title);
        list=new ArrayList<>();
        Intent intent=getIntent();
        title=intent.getStringExtra("title");
        textView.setText(title);
        contentType=intent.getStringExtra("contentType");
        key=intent.getStringExtra("key");
        loadDatas(pageSize);
        adapter=new DiscoverRankSecondAnchorAdapter(this,list);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);
    }
    public void loadDatas(int pageSize){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.RANK_SECOND_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        DiscoverRankSecondAnchorService service=retrofit.create(DiscoverRankSecondAnchorService.class);
        service.getDatas(contentType,"android",key,1,pageSize,2).enqueue(this);

    }

    @Override
    public void onResponse(Call<DiscoverRankSecondAnchorEntity> call, Response<DiscoverRankSecondAnchorEntity> response) {
        String title=response.body().getTitle();
        MyLog.i("SSSSSSSSSS",title);
        totalCount=response.body().getTotalCount();
        list.removeAll(list);
        list.addAll(response.body().getList());
        MyLog.i("SSSSSSSSSSS",list.get(3).getPersonDescribe());
        adapter.notifyDataSetChanged();
       // swipeRefreshLayout.setRefreshing(false);
        //isLoadMore=true;
    }

    @Override
    public void onFailure(Call<DiscoverRankSecondAnchorEntity> call, Throwable t) {

    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if(isBottom&&i==SCROLL_STATE_IDLE&&pageSize<totalCount){
            pageSize+=20;
            loadDatas(pageSize);
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if(i+i1==i2){
            isBottom=true;
        }else {
            isBottom=false;
        }
    }
    public void onClick(View view){
        finish();
    }
}
